#pragma once

#include <iostream>
#include <string>
#include <pthread.h>
#include <ctime>

typedef void (*callback_t)(void*);

static int num = 1;

class Thread
{
public:
    Thread(callback_t cb,void* args)
    :_tid_(0), name_(""),start_timestamp_(0),isrunning_(false),cb_(cb),args_(args)
    {}

    static void* Routine(void* args)
    {
        Thread* thread = static_cast<Thread*> (args);
        thread->Entery();
        return nullptr;
    }

    pthread_t Gettid()
    {
        return _tid_;
    }

    void Run()
    {
        name_ = "thread-" + std::to_string(num++);
        start_timestamp_ = time(nullptr);
        isrunning_ = true;
        pthread_create(&_tid_, nullptr, Routine, this);
    }

    void Join()
    {
        pthread_join(_tid_,nullptr);
        isrunning_ = false;
    }

    std::string Name()
    {
        return name_;
    }

    uint64_t StartTimeStamp()
    {
        return start_timestamp_;
    }

    bool IsRunning()
    {
        return isrunning_;
    }

    void Entery()
    {
        cb_(args_);
    }

    ~Thread()
    {}

private:
    pthread_t _tid_;
    std::string name_;
    uint64_t start_timestamp_;
    bool isrunning_;
    callback_t cb_;
    void* args_;
};